//
//  RoutableManager.m
//  ZHLY
//
//  Created by 金忠俊 on 16/11/18.
//  Copyright © 2016年 inphase. All rights reserved.
//

#import "RoutableManager.h"

@implementation RoutableManager

+(void)load {
    
    NSDictionary *routableMap = @{kCategoryExpandRoute:@"CategoryExpandViewController"};
    
    for (NSString *key in [routableMap allKeys]) {
        [[Routable sharedRouter] map:key toController:NSClassFromString(routableMap[key])];
    }
}

+(void)open:(NSString *)route animated:(BOOL)animated {
    
    UINavigationController *navi = [Routable sharedRouter].navigationController;
    
    if (!navi) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        UITabBarController *tab = (UITabBarController *)keyWindow.rootViewController;
        UINavigationController *navi = [tab.viewControllers objectAtIndex:tab.selectedIndex];
        [Routable sharedRouter].navigationController = navi;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([route class] == [NSNull class] || [route isEqualToString:@""] || !route) {
                return;
            }
            if ([route hasPrefix:@"http"]) {
                NSString *urlParameter = [route stringByReplacingOccurrencesOfString:@"/" withString:@"\\"];
                [[Routable sharedRouter] open:[NSString stringWithFormat:@"html/%@",urlParameter] animated:animated];
            }else {
                [[Routable sharedRouter] open:route animated:animated];
            }
            [Routable sharedRouter].navigationController = nil;
        });
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([route class] == [NSNull class] || [route isEqualToString:@""] || !route) {
                return;
            }
            if ([route hasPrefix:@"http"]) {
                NSString *urlParameter = [route stringByReplacingOccurrencesOfString:@"/" withString:@"\\"];
                [[Routable sharedRouter] open:[NSString stringWithFormat:@"html/%@",urlParameter] animated:animated];
            }else {
                [[Routable sharedRouter] open:route animated:animated];
            }
        });
    }
}

@end
